//
//  UIImage+CoreImage.m
//  DemoBlurryImage
//
//  Created by 唐可雄 on 16/9/2.
//  Copyright © 2016年 swustyc. All rights reserved.
//

#import "UIImage+CoreImage.h"

@implementation UIImage (CoreImage)

+ (instancetype)blurImageOf:(UIImage *)image {
    // 校验数据合法性
    if (image == nil) {
        return nil;
    }
    
    // 1.创建CIImage
    CIImage *ciImage = [[CIImage alloc] initWithImage:image];
    // 2.创建滤镜CIFilter
    CIFilter *blurFilter = [CIFilter filterWithName:@"CIGaussianBlur"];
    // 2.1.将CIImage输入到滤镜中
    [blurFilter setValue:ciImage forKey:kCIInputImageKey];
    // 2.2设置模糊度
    [blurFilter setValue:@(2) forKey:@"inputRadius"];
    // 2.3将处理好的图片输出
    CIImage *outCiImage = [blurFilter valueForKey:kCIOutputImageKey];
    // 3.CIContext(option参数为nil代表用CPU渲染,若想用GPU渲染请查看此参数)
    CIContext *context = [CIContext contextWithOptions:nil];
    // 4.获取CGImage句柄
    CGImageRef outCGImage = [context createCGImage:outCiImage fromRect:[outCiImage extent]];
    // 5.获取最终的图片
    UIImage *blurImage = [UIImage imageWithCGImage:outCGImage];
    // 6.释放CGImage
    CGImageRelease(outCGImage);
    
    return blurImage;
}

@end
